package cn.dida.dao;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import cn.dida.model.OptLog;
import cn.dida.model.Pageable;

/**
 * 操作日志 数据交互类
 * @author weiwei
 */
@Repository
public class OptLogDao extends DaoBase {
	/**
	 * 操作日志列表
	 * @param companyId 公司id
	 * @param keyword 查询条件
	 * @param timeLimitStart 操作时间(开始)
	 * @param timeLimitEnd 操作时间(结束)
	 * @param page 分页游标
	 * @return Pageable<OptLog> 操作日志分页列表
	 */
    public Pageable<OptLog> queryLog(Integer companyId, String keyword, Long timeLimitStart, Long timeLimitEnd, int page) {
        Object[] args = {};
        StringBuffer sqlQueryResultCount = new StringBuffer("select count(1) from opt_log t1 join company t2 on t1.company_id = t2.id left join staff t3 on t1.staff_id = t3.id");
        StringBuffer sqlQueryResult = new StringBuffer("select t1.*, t2.name 'companyName',t3.account 'staffAccount' from opt_log t1 join company t2 on t1.company_id = t2.id left join staff t3 on t1.staff_id = t3.id");

        StringBuffer sqlWhere = new StringBuffer(" where t1.company_id = ?");
        args = ArrayUtils.add(args, companyId);
        if (StringUtils.isNotBlank(keyword)) {
            sqlWhere.append(" and (t2.name like ? or t3.account = ?)");
            args = ArrayUtils.add(args, "%" + keyword + "%");
            args = ArrayUtils.add(args, keyword);
        }
        if (timeLimitStart != null) {
            sqlWhere.append(" and t1.time >= ?");
            args = ArrayUtils.add(args, timeLimitStart);
        }
        if (timeLimitEnd != null) {
            sqlWhere.append(" and t1.time <= ?");
            args = ArrayUtils.add(args, timeLimitEnd);
        }

        // 查询记录总数
        Integer resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.append(sqlWhere).toString(), Integer.class, args);
        // 查询记录
        sqlQueryResult.append(sqlWhere).append("  order by t1.time desc limit ?,?");
        args = ArrayUtils.add(args, (page - 1) * getPageSize());
        args = ArrayUtils.add(args, getPageSize());

        List<OptLog> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(OptLog.class));
        return new Pageable<>(page, getPageSize(), resultTotal, result);
    }

    /**
     * 新增操作日志
     * @param optLog 操作日志实体类
     * @return int 受影响的记录行数
     */
    public int insert(OptLog optLog) {
        String sql = "insert into opt_log(staff_id, company_id, time, `event`, outline, detail, target) values(:staff_id, :company_id, :time, :event, :outline, :detail, :target)";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("staff_id", optLog.getStaffId());
        paramMap.put("company_id", optLog.getCompanyId());
        paramMap.put("time", optLog.getTime());
        paramMap.put("event", optLog.getEvent());
        paramMap.put("outline", optLog.getOutline());
        paramMap.put("detail", optLog.getDetail());
        paramMap.put("target", optLog.getTarget());
        return namedParameterJdbcTemplate.update(sql, paramMap);
    }

}